package com.next

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_COMPANY_USER'])
@Transactional(readOnly = true)
class TerritoryController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond Territory.list(params), model: [territoryCount: Territory.count()]
    }

    def show(Territory territory)
    {
        def territoryTeams = TerritoryTeam.findAllByTerritory(territory)
        respond territory, model: [territoryTeams: territoryTeams]
    }

    def create()
    {
        respond new Territory(params)
    }

    @Transactional
    def save(Territory territory)
    {
        if (territory == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (territory.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond territory.errors, view: 'create'
            return
        }

        territory.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'territory.label',
                                                                                        default: 'Territory'),
                    territory.id])
                redirect territory
            }
            '*' { respond territory, [status: CREATED] }
        }
    }

    def edit(Territory territory)
    {
        respond territory
    }

    @Transactional
    def update(Territory territory)
    {
        if (territory == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (territory.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond territory.errors, view: 'edit'
            return
        }

        territory.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'territory.label',
                                                                                        default: 'Territory'),
                    territory.id])
                redirect territory
            }
            '*' { respond territory, [status: OK] }
        }
    }

    @Transactional
    def delete(Territory territory)
    {

        if (territory == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        territory.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'territory.label',
                                                                                        default: 'Territory'),
                    territory.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'territory.label',
                                                                                          default: 'Territory'),
                    params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }

    def searchTerritory()
    {
        def name = params['name']
        def territoryList = Territory.findAll("from Territory where name like '%${name}%'", [max: params.max ?: 10, offset: params.offset ?: 0])
        def count = Territory.executeQuery("select count(id) from Territory where name like '%${name}%'")
        respond territoryList, model: [territoryCount: count[0]], view: 'index'
    }
}
